<template>
    <base-breadcrumb :title="$route.meta.title" :breadcrumb="!isAudit">
        <!-- 党务监督编辑 -->
        <a-form-model
            ref="ruleForm"
            :model="form"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            :rules="editModel != 0 ? rules : {}"
        >
            <a-spin :spinning="loading.SUBMITTED || loading.DRAFT || loading.REJECTED || loading.PAST || editMaterial">
                <!-- 表格 -->
                <a-card :bordered="false">
                    <title-name title="基本信息"></title-name>
                    <custom-form
                        ref="customForm"
                        :formList="formList"
                        :form="form"
                        :editModel="editModel == 0 ? '' : ''"
                        @change="change"
                    >
                    </custom-form>
                </a-card>
                <a-card :bordered="false">
                    <!-- 附件上传 -->
                    <title-name title="八张表"></title-name>
                    <a-row :gutter="24">
                        <a-col :md="24" :sm="24" v-if="editModel != 0">
                            <a-form-model-item label="*附件" prop="eightFileInfos">
                                <l-table businessCode="OF05" :uploadArrys.sync="form.eightFileInfos"></l-table>
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" v-else>
                            <a-form-model-item label="*附件">
                                <file-link :fileList="form.eightFileInfos"></file-link> </a-form-model-item
                        ></a-col>

                        <a-col :md="24" :sm="24">
                            <div class="file_prompt ml20">
                                支持格式：.rar .zip doc. docx. xls. pdf. jpg .png，单个附件大小不得超过200M
                            </div>
                        </a-col>
                    </a-row>
                    <!-- </a-form-model> -->
                </a-card>
                <a-card :bordered="false" v-if="isDisabled()">
                    <!-- 附件上传 -->
                    <title-name title="整改材料"></title-name>
                    <a-row :gutter="24">
                        <a-col :md="24" :sm="24" v-if="isDisabled()">
                            <a-form-model-item label="*附件" prop="reportFileInfos">
                                <l-table businessCode="OF05" :uploadArrys.sync="form.reportFileInfos"></l-table>
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" v-else>
                            <a-form-model-item label="*附件">
                                <file-link :fileList="form.reportFileInfos"></file-link> </a-form-model-item
                        ></a-col>

                        <a-col :md="24" :sm="24">
                            <div class="file_prompt ml20">
                                支持格式：.rar .zip doc. docx. xls. pdf. jpg .png，单个附件大小不得超过200M
                            </div>
                        </a-col>
                    </a-row>
                    <!-- </a-form-model> -->
                </a-card>
            </a-spin>
            <!-- 页脚操作栏 -->
            <!-- 页脚操作栏 -->
            <div class="tool-btns mb20 page-btn-right-top" v-if="editModel != 0">
                <a-button
                    :loading="loading.REJECTED || loading.PAST"
                    v-if="form.auditStatus == 'REJECTED' || form.auditStatus == 'PAST'"
                    :disabled="loading.SUBMITTED || loading.DRAFT"
                    type="primary"
                    class="button_right"
                    @click="handleSubmit(form.auditStatus)"
                    >保存</a-button
                >

                <a-button
                    :loading="loading.DRAFT"
                    :disabled="loading.SUBMITTED || loading.REJECTED"
                    v-else
                    type="primary"
                    class="button_right"
                    @click="handleSubmit('DRAFT')"
                    >保存草稿</a-button
                >

                <a-button
                    v-if="form.auditStatus != 'PAST'"
                    :loading="loading.SUBMITTED"
                    :disabled="loading.REJECTED || loading.DRAFT || loading.PAST"
                    type="primary"
                    class="button_right ml20"
                    @click="handleSubmit('SUBMITTED')"
                    >提交</a-button
                >
                <a-button class="ml20" type="primary" @click="handleClose">关闭</a-button>
            </div>
            <div class="tool-btns mb20 page-btn-right-top" v-else>
                <a-button
                    v-if="report"
                    :loading="editMaterial"
                    type="primary"
                    class="button_right"
                    @click="handleSubmit(form.auditStatus)"
                    >保存</a-button
                >
                <a-button class="ml20" type="primary" @click="handleClose">关闭</a-button>
            </div>
        </a-form-model>
        <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
</template>

<script>
import {
    getProjectSupervision,
    updateProjectSupervision,
    addProjectSupervision
} from '@/api/disciplineInspection/supervision' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import Selector from '@/components/Select/Selector'

import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import SelectOrgPeople from '@/components/Select/SelectOrgPeople'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
export default {
    name: 'a' + Date.now(),
    extends: new BaseEditPage(),
    components: {
        SelectSubCompany,
        SelectOrgPeople
    },
    data() {
        return {
            customRender: (q) => {
                let result = q.name
                if (q.postPNames && q.postPNames.length) {
                    result = `${result} (${q.postPNames.join('/')}) `
                }
                return result
            },
            loading: { DRAFT: false, REJECTED: false, SUBMITTED: false, PAST: false }, // 草稿和提交按钮loding
            editMaterial: false,
            editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
            form: {
              month:''
            }, // 表单
            rules: {
                projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
                companyId: [{ required: true, message: '请选择分公司位', trigger: 'change' }],
                month: [{ required: true, message: '请选择月份', trigger: 'change' }],
                eightFileInfos: [{ type: 'array', required: true, message: '请上传附件', trigger: 'blur' }],
                reportFileInfos: [{ type: 'array', required: true, message: '请上传附件', trigger: 'blur' }]
            }, // 校验规则
            init: false, // 初始化控件
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    props: {
        isAudit: {
            // 是否作为审批详情
            userId: Boolean,
            default: false
        }
    },
    watch: {
        // 监听附件实现附件校验
        'form.eightFileInfos'() {
            if (this.editModel != 0 && this.init) {
                this.$nextTick(() => {
                    this.$refs.ruleForm?.validateField('eightFileInfos')
                })
            }
        },
        // 监听附件实现附件校验
        'form.reportFileInfos'() {
            if (this.editModel != 0 && this.init) {
                this.$nextTick(() => {
                    this.$refs.ruleForm?.validateField('reportFileInfos')
                })
            }
        },
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.$nextTick(() => {
                        this.editModel = '0'
                    })
                }
            }
        }
    },
   async mounted() {
        this.$route.meta.title = ['项目监督月报查看', '项目监督月报编辑', '项目监督月报新增'][this.editModel]
        // 不为添加时请求详情
        if (this.editModel != 2) {
            this.loading.DRAFT = true
            this.loading.REJECTED = true
            this.loading.SUBMITTED = true
            getProjectSupervision(this.id)
                .then((res) => {
                    this.loading.DRAFT = false
                    this.loading.REJECTED = false
                    this.loading.SUBMITTED = false
                    this.form = res.data

                    this.$nextTick(() => {
                        this.init = true
                    })
                })
                .catch(() => {
                    this.loading.DRAFT = false
                    this.loading.REJECTED = false
                    this.loading.SUBMITTED = false
                })
        } else {
            let date = new Date()

            if (date.getMonth() + 1 < 10) {
                this.form.month = date.getFullYear() + '-0' + (date.getMonth() + 1)
            } else {
                this.form.month = date.getFullYear() + '-' +  (date.getMonth() + 1)
            }
            console.log(this.form.month)
            this.$nextTick(() => {
                this.init = true
            })
        }
       //获取通过获取流程配置值
       this.processConfig = await this.$getConfigFn('supervise_prject_month_report')
    },
    computed: {
        formList() {
            return [
                {
                    keyName: 'month',
                    type: 'a-month-picker',
                    placeholder: '请选择日期',
                    disabled: this.editModel == 0,
                    label: '月份',
                    attrs: {
                        valueFormat: 'YYYY-MM'
                    }
                },
                {
                    type: SelectSubCompany,
                    keyName: this.editModel == 0 ? 'branchComName' : 'branchComId',
                    label: '所属分公司',
                    disabled: true,
                    attrs: {
                        ref: 'SelectSubCompany',
                        autoSelected: this.editModel == 2
                    }
                },
                {
                    type: SelectProject,
                    keyName: this.editModel == 0 ? 'projectName' : 'projectId',
                    label: '所属项目',
                    disabled: true,
                    attrs: {
                        ref: 'SelectProject',
                        autoSelected: this.editModel == 2,
                        keyValue: 'branchComId',
                        isClear: this.editModel == 2,
                        form: this.form
                    }
                }
            ]
        }
    },
    methods: {
        isDisabled() {
            let flag = false
            if (this.editModel == 0 && this.form.reportFileInfos?.length > 0) {
                flag = true
            }
            if (this.report) {
                flag = true
            }
            console.log(flag)
            return flag
        },
        change(name, e) {
            if (name == 'branchComId') {
                console.log(this.$refs)
                this.form.branchComName = this.$refs.customForm?.$refs.ruleForm?.$refs.SelectSubCompany?.getValueName(e)
            } else if (name == 'projectId') {
                this.form.projectName = this.$refs.customForm?.$refs.ruleForm?.$refs.SelectProject?.getValueName(e)
            }
            console.log(this.form)
        },
        handleClose() {
            if (this.isAudit) {
                window.close()
            } else {
                this.$multiTab.close(this.$route.fullpath)
            }
        },
        /**
         * 提交表单
         */
        handleSubmit(auditStatus) {
            let that = this
            that.setProcessConfig = null;
            this.submitApprovalConfirm(auditStatus =='SUBMITTED'?this.processConfig:6, (flag)=>{
              if(!flag) {
              this.loading[auditStatus] = false
              return
            }
           let api = [updateProjectSupervision, addProjectSupervision][that.editModel - 1] // 根据操作切换url地址
            // 校验表单
            that.$refs.ruleForm.validate((e) => {
                if (e) {
                    // 提交表单
                    that.loading[auditStatus] = true
                    that.form.auditStatus = auditStatus
                    let form = that.$clone(that.form)
                    if (that.report) {
                        api = updateProjectSupervision
                        that.editMaterial = true
                    }
                    api(form)
                        .then((res) => {
                            if (auditStatus == 'SUBMITTED') {
                                that.$message.success('提交成功！')
                                //提交时，返回id
                                this.businessId = res.data || this.id;
                                this.setProcessConfig = this.processConfig;
                                
                            } else {
                                that.$message.success('保存成功！')
                                that.$multiTab.close()
                            }
                            that.loading[auditStatus] = false
                            that.editMaterial = false
                        })
                        .catch(() => {
                            that.loading[auditStatus] = false
                            that.editMaterial = false
                        })
                }
            })
            })
  
        }
    }
}
</script>

<style lang="less" scoped>
.file_prompt {
    color: #999;
}
</style>
